Return to doc.sitecore.com

Get Published Items

This article describes how to display the list of published items right after the publishing has finished. There are two ways to do this:

  1. Use PublishLog
    1. Derive a new class from PublishLog class and override necessary methods
    2. Add custom event handler to “publish:begin” event and hook custom PublishLog to the publisher instead of native PublishLog.
  2. Use PublishEngine
    1. Create a class with appropriate event handlers
    2. Create a hook where you can assign an event handler to corresponding PublishEngine.

The present article describes the latter approach. The former approach is outlined here:

http://sdn.sitecore.net/Scrapbook/Change%20items%20during%20the%20publishing%20process.html

1.  Creating a class with event handlers

The following events can be used to track the changes made by the publishing process. 

You can use one of the methods listed above or their combination to achieve the desired functionality.

The example below shows how to catch all items which have been added to or changed in the web database. Algorithm notes: the code gets the items which have been added to or changed in web database by catching the VersionAdded event. In this way the code catches all changes made to the last accessible version of an item. For instance, the ItemAdded event of PublishEngine class is fired only when a newly created item is published to web database. This method will not be fired if a new version of an item is created and published.
So, we will use the VersionAdded event.

using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.Data.Items;
 
namespace Statistics.Publish
{
   public class GetPublishedItems
   {
      internal static void PublishEngine_VersionAdded(object sender, Sitecore.Data.Events.VersionAddedEventArgs args)
      {
         Item version = args.Item;
         Log.Info(string.Format("Item: {0}, Version: {1}, Language: {2}", version.Paths.Path, version.Version.Number, version.Language.Name), this);
      }
 
   }
}

Create a hook:

using Sitecore.Data;
 
namespace Statistics.Publish
{
   class PublishEngineHook
   {
      public void Initialize()
      {
         Database database = Sitecore.Context.ContentDatabase;
         if (database == null)
         {
            database = Sitecore.Context.Database;
         }
         if (database != null)
         {
            database.Engines.PublishEngine.VersionAdded += new EventHandler<Sitecore.Data.Events.VersionAddedEventArgs>(Statistics.Publish.GetPublishedItems.PublishEngine_VersionAdded);
         }
      }
   }

Compile your solution and add hook to the web.config file under the <hooks> section as shown below:

    <hooks>
      .............
      <hook type="Statistics.Publish.PublishEngineHook, GetPublishedItems" />
    </hooks> 

Restart the IIS site to make Sitecore initialize the hook.

Related reading:

http://sdn.sitecore.net/Articles/Customizing%20Sitecore%20Client/Events%20Hooks%20Pipelines/Hooks.html